﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReadeAssetManager
{
    public partial class frmAssetSearch : Form
    {
        private const string NO_MATCH = "There were no assets matching your search parameters.";
        private delegateReturnObject _returnobj;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="AssetList">List<AMSServiceRef.Asset></param>
        public frmAssetSearch(List<AMSServiceRef.Asset> AssetList, delegateReturnObject ReturnObjectMethod)
        {
            InitializeComponent();
            PopulateListView(AssetList);
            _returnobj = ReturnObjectMethod;
        }

        /// <summary>
        /// PopulateListView
        /// This method populates the list view with assets from the AssetList.  If there are no assets in
        /// the list, this method instead adds a dummy ListViewItem indicating that no results were found.
        /// </summary>
        /// <param name="AssetList">List<AMSServiceRef.Asset></param>
        private void PopulateListView(List<AMSServiceRef.Asset> AssetList)
        {
            if (AssetList.Count > 0)
            {
                FormatListView();
                foreach (AMSServiceRef.Asset a in AssetList)
                {
                    lvAssets.Items.Add(CreateListViewItem(InstantiateLists(a)));
                }
            }
            else
            {
                lvAssets.Columns.Add("", lvAssets.Width - 10);
                lvAssets.HeaderStyle = ColumnHeaderStyle.None;
                lvAssets.Items.Add(new ListViewItem(NO_MATCH));
                lvAssets.View = View.Details;
            }
        }

        /// <summary>
        /// CreateListViewItem
        /// This method is responsible for fully populating and returning a ListViewItem
        /// to represent the specified AMSServiceRef.Asset object.
        /// </summary>
        /// <param name="CurrentAsset">AMSServiceRef.Asset</param>
        /// <returns>ListViewItem</returns>
        private ListViewItem CreateListViewItem(AMSServiceRef.Asset CurrentAsset)
        {
            ListViewItem lvi = new ListViewItem(new string[] {CurrentAsset.Name, 
                                                              CurrentAsset.Manufacturer,
                                                              CurrentAsset.Model,
                                                              CurrentAsset.Year.ToShortDateString(),
                                                              CurrentAsset.Purchases.Count.ToString(),
                                                              CurrentAsset.Schedules.Count.ToString(),
                                                              CurrentAsset.Services.Count.ToString(),
                                                              CurrentAsset.Warranties.Count.ToString(),
                                                              CurrentAsset.Images.Count.ToString() });

            lvi.Tag = CurrentAsset;

            return lvi;
        }

        /// <summary>
        /// FormatListView
        /// Formats the lvAssets control by setting columns.
        /// </summary>
        private void FormatListView()
        {
            lvAssets.Columns.Add("Name");
            lvAssets.Columns.Add("Manufacturer");
            lvAssets.Columns.Add("Model");
            lvAssets.Columns.Add("Date");
            lvAssets.Columns.Add("Num Purch");
            lvAssets.Columns.Add("Num Sched");
            lvAssets.Columns.Add("Num Serv");
            lvAssets.Columns.Add("Num Warr");
            lvAssets.Columns.Add("Num Images");
            lvAssets.HeaderStyle = ColumnHeaderStyle.Clickable;
            lvAssets.View = View.Details;
        }

        /// <summary>
        /// InstantiateLists
        /// Method to make sure that all Lists in the specified AMSServiceRef.Asset
        /// object have been instantiated.  This is necessary to show the number
        /// of items in each list in the search results.  Due to the WCF serialization
        /// process, some Lists can remain uninstantiated if no values are present.
        /// While there are other methods in this application to fix this, I want to
        /// make sure that nothing snuck through.
        /// </summary>
        /// <param name="CurrentAsset">AMSServiceRef.Asset</param>
        /// <returns>AMSServiceRef.Asset</returns>
        private AMSServiceRef.Asset InstantiateLists(AMSServiceRef.Asset CurrentAsset)
        {
            if (CurrentAsset.Images == null)
                CurrentAsset.Images = new List<AMSServiceRef.AMSImage>();
            if (CurrentAsset.Purchases == null)
                CurrentAsset.Purchases = new List<AMSServiceRef.PurchaseData>();
            if (CurrentAsset.Schedules == null)
                CurrentAsset.Schedules = new List<AMSServiceRef.ScheduleData>();
            if (CurrentAsset.Services == null)
                CurrentAsset.Services = new List<AMSServiceRef.ServiceData>();
            if (CurrentAsset.Warranties == null)
                CurrentAsset.Warranties = new List<AMSServiceRef.WarrantyData>();

            return CurrentAsset;
        }

        /// <summary>
        /// lvAssets_DoubleClick
        /// Passes the selected asset back to frmMain so it can be opened in frmAsset.
        /// </summary>
        /// <param name="sender">[object] The lvAssets control.</param>
        /// <param name="e">[EventArgs] The standard event arguments for a DoubleClick event.</param>
        private void lvAssets_DoubleClick(object sender, EventArgs e)
        {
            if (lvAssets.SelectedItems.Count > 0)
            {
                _returnobj(((ListViewItem)lvAssets.SelectedItems[0]).Tag);
            }
        }
    }
}
